/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lvoddownloader;

import LPlayerUILib.JImagePanel;
import Lvod.DownLoader;
import java.awt.Color;
import java.util.Locale;
import javax.swing.ImageIcon;

/**
 *
 * @author wcss
 */
public class LDownLoadItem extends JImagePanel {

    public static final int stopDownload = 0;
    public static final int startDownload = 1;
    public static final int searchDownloadPeer = 2;
    public static final int downloadFinish = 3;
    private Color releaseColor = new Color(31, 32, 37);
    private Color pressColor = new Color(53, 138, 203);
    public String downloadUrl = "";
    public String bufferUrl = "";
    public String localUrl = "";
    public String movieShowName = "";
    public String movieInfoText = "";
    public int downloadProgress = 0;
    public int currentState = 0;
    private ImageIcon noSelectImage = null;
    private ImageIcon selectImage = null;
    public DownLoader downloaderObj = null;
    private Boolean isStartDownload = false;
    public LDownloadListActionInterface actionsListener = null;

    /**
     * Creates new form LDownLoadItem
     */
    public LDownLoadItem() 
    {
        initComponents();
        noSelectImage = new ImageIcon(LDownLoadItem.class.getResource("/UIImages/playerbgs/downloaditembg-1.JPG"));
        selectImage = new ImageIcon(LDownLoadItem.class.getResource("/UIImages/playerbgs/downloaditemselected_bg.JPG"));
        this.selectLeave();
        this.pbProgress.setUI(new LProgressBarUI());
        this.checkLayouts();
        this.btnPause.setVisible(false);
    }
    
    public void setnoSelectImageIndex(String sindex)
    {
        if (sindex.equals("1"))
        {
            releaseColor = new Color(21,22,26);
        }else
        {
            releaseColor = new Color(31, 32, 37);
        }
        noSelectImage = new ImageIcon(LDownLoadItem.class.getResource("/UIImages/playerbgs/downloaditembg-" + sindex + ".JPG"));
    }

    public void setMovieName(String sname) {
        this.movieShowName = sname;
        this.lblMovieName.setText(this.movieShowName);
        checkLayouts();
    }

    public void setMovieInfo(String itext) {
        this.movieInfoText = itext;
        this.lblMovieSize.setText(this.movieInfoText);
        checkLayouts();
    }

    public void setDownloadProgress(int curvalue) {
        this.downloadProgress = curvalue;
        this.pbProgress.setMaximum(100);
        this.pbProgress.setValue(this.downloadProgress);
        checkLayouts();
    }

    public void selectEnter() {
        this.pbProgress.setValue(this.downloadProgress);
        this.pbProgress.setBackground(pressColor);
        this.backgroundImageObj = this.selectImage;
//        this.plBtn.backgroundImageObj = this.selectImage;
//        this.plInfo.backgroundImageObj = this.selectImage;
        this.repaint();
        this.pbProgress.repaint();
    }

    public void selectLeave() {
        this.pbProgress.setValue(this.downloadProgress);
        this.pbProgress.setBackground(releaseColor);
        this.backgroundImageObj = this.noSelectImage;
//        this.plBtn.backgroundImageObj = this.noSelectImage;
//        this.plInfo.backgroundImageObj = this.noSelectImage;
        this.repaint();
        this.pbProgress.repaint();
    }

    public void startDownload() {
        isStartDownload = true;
        this.btnStartDownload.buttonText = "停止";
        this.btnStartDownload.repaint();
    }
    
    public void waitDownload()
    {
        isStartDownload = false;
        this.btnStartDownload.buttonText = "等待";
        this.btnStartDownload.repaint();
    }

    public void stopDownload() {
        isStartDownload = false;
        this.btnStartDownload.buttonText = "下载";
        this.btnStartDownload.repaint();
    }

    public void checkLayouts() {
        this.lblMovieName.setBounds(0, 0, (this.getWidth() / 3) * 2, this.getHeight() / 2);
        this.lblMovieSize.setBounds((this.getWidth() / 3) * 2, 0, this.getWidth() / 3, this.getHeight() / 2);

        this.btnPlay.setBounds((((this.getWidth() / 3) - 48) / 2), (this.getHeight() / 2) + (((this.getHeight() / 2) - 22) / 2), 48, 22);
        this.btnPause.setBounds((((this.getWidth() / 3) - 48) / 2), (this.getHeight() / 2) + (((this.getHeight() / 2) - 22) / 2), 48, 22);
        this.btnStartDownload.setBounds((this.getWidth() / 3) * 2 + (((this.getWidth() / 3) - 48) / 2), (this.getHeight() / 2) + (((this.getHeight() / 2) - 22) / 2), 48, 22);
        this.pbProgress.setBounds(this.getWidth() / 3, this.getHeight() / 2, this.getWidth() / 3, this.getHeight() / 2);
        //this.revalidate();        
    }

    public void processActions(String action, String args) {
        if (actionsListener != null) {
            actionsListener.processAction(this, action, args);
        }
    }

    public void setPlayButton(Boolean value) 
    {
        this.btnPlay.setVisible(value);
    }

    public void setDownLoadButton(Boolean value) {
        this.btnStartDownload.setVisible(value);
//        if (value)
//        {
//            this.btnStartDownload.setLocation((int)this.btnStartDownload.getLocation().getX(), (int)this.btnStartDownload.getLocation().getY() - 100);
//        }else
//        {
//            this.btnStartDownload.setLocation((int)this.btnStartDownload.getLocation().getX(), (int)this.btnStartDownload.getLocation().getY() + 100);
//        }
    }
    
    public void hideDownLoadButton()
    {
        this.btnStartDownload.setLocation((int)this.btnStartDownload.getLocation().getX(), (int)this.btnStartDownload.getLocation().getY() + 100);
    }

    public void setPauseButton(Boolean value) 
    {
       this.btnPause.setVisible(value);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblMovieName = new javax.swing.JLabel();
        lblMovieSize = new javax.swing.JLabel();
        btnPlay = new LPlayerUILib.JImageButton("/UIImages/video/star-1.png","/UIImages/video/star-3.png","/UIImages/video/star-2.png","",true,true);
        pbProgress = new javax.swing.JProgressBar();
        btnStartDownload = new LPlayerUILib.JImageButton("/UIImages/video/Download-1.png","/UIImages/video/Download-3.png","/UIImages/video/Download-2.png","下载",true,true);
        btnPause = new LPlayerUILib.JImageButton("/UIImages/video/star_stop-1.png","/UIImages/video/star_stop-3.png","/UIImages/video/star_stop-2.png","",true,true);

        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });
        setLayout(null);

        lblMovieName.setFont(lblMovieName.getFont());
        lblMovieName.setForeground(java.awt.Color.white);
        lblMovieName.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblMovieName.setText("空");
        add(lblMovieName);
        lblMovieName.setBounds(0, 0, 130, 37);

        lblMovieSize.setFont(lblMovieSize.getFont());
        lblMovieSize.setForeground(java.awt.Color.white);
        lblMovieSize.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblMovieSize.setText("0分钟");
        add(lblMovieSize);
        lblMovieSize.setBounds(130, 0, 90, 37);

        btnPlay.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnPlayMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout btnPlayLayout = new javax.swing.GroupLayout(btnPlay);
        btnPlay.setLayout(btnPlayLayout);
        btnPlayLayout.setHorizontalGroup(
            btnPlayLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 48, Short.MAX_VALUE)
        );
        btnPlayLayout.setVerticalGroup(
            btnPlayLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 22, Short.MAX_VALUE)
        );

        add(btnPlay);
        btnPlay.setBounds(0, 40, 48, 22);

        pbProgress.setValue(60);
        pbProgress.setBorder(null);
        pbProgress.setBorderPainted(false);
        add(pbProgress);
        pbProgress.setBounds(50, 40, 120, 22);

        btnStartDownload.setForeground(java.awt.Color.white);
        btnStartDownload.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnStartDownloadMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout btnStartDownloadLayout = new javax.swing.GroupLayout(btnStartDownload);
        btnStartDownload.setLayout(btnStartDownloadLayout);
        btnStartDownloadLayout.setHorizontalGroup(
            btnStartDownloadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 50, Short.MAX_VALUE)
        );
        btnStartDownloadLayout.setVerticalGroup(
            btnStartDownloadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 22, Short.MAX_VALUE)
        );

        add(btnStartDownload);
        btnStartDownload.setBounds(170, 40, 50, 22);

        btnPause.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnPauseMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout btnPauseLayout = new javax.swing.GroupLayout(btnPause);
        btnPause.setLayout(btnPauseLayout);
        btnPauseLayout.setHorizontalGroup(
            btnPauseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 48, Short.MAX_VALUE)
        );
        btnPauseLayout.setVerticalGroup(
            btnPauseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 22, Short.MAX_VALUE)
        );

        add(btnPause);
        btnPause.setBounds(0, 0, 48, 22);
    }// </editor-fold>//GEN-END:initComponents

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
        // TODO add your handling code here:
        checkLayouts();
    }//GEN-LAST:event_formComponentResized

    private void btnPlayMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnPlayMouseClicked
        // TODO add your handling code here:
        selectEnter();
        checkLayouts();
        processActions(ActionLists.playMovie, "");
    }//GEN-LAST:event_btnPlayMouseClicked

    private void btnStartDownloadMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnStartDownloadMouseClicked
        // TODO add your handling code here:
        selectEnter();
        if (isStartDownload) {
            stopDownload();
            processActions(ActionLists.stopDownload, "");
        } else {
            startDownload();
            processActions(ActionLists.startDownload, "");
        }
        checkLayouts();
    }//GEN-LAST:event_btnStartDownloadMouseClicked

    private void btnPauseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnPauseMouseClicked
        // TODO add your handling code here:
        selectEnter();
        checkLayouts();
        processActions(ActionLists.pauseMovie, "");
    }//GEN-LAST:event_btnPauseMouseClicked

    /**
     * 点击开始下载按钮
     */
    public void clickStartDownloadButton()
    {
        btnStartDownloadMouseClicked(null);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private LPlayerUILib.JImageButton btnPause;
    private LPlayerUILib.JImageButton btnPlay;
    private LPlayerUILib.JImageButton btnStartDownload;
    private javax.swing.JLabel lblMovieName;
    private javax.swing.JLabel lblMovieSize;
    private javax.swing.JProgressBar pbProgress;
    // End of variables declaration//GEN-END:variables
}
